package com.ljx.qqpro.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ljx.qqpro.common.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ljx.qqpro.entity.ContactsGroupEntity;
import com.ljx.qqpro.service.ContactsGroupService;
import com.ljx.qqpro.common.utils.PageUtil;
import com.ljx.qqpro.common.utils.Result;



/**
 * 联系人分组表
 *
 * @author LiJunXi
 * @email 2770063826@qq.com
 * @date 2023-10-18 17:28:14
 */
@RestController
@RequestMapping("/contactsgroup")
public class ContactsGroupController {
    @Autowired
    private ContactsGroupService contactsGroupService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public Result list(@RequestParam Map<String, Object> params){
        PageUtil page = contactsGroupService.queryPage(params);

        return Result.success(page, "page");
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id){
		ContactsGroupEntity contactsGroup = contactsGroupService.getById(id);

        return Result.success(contactsGroup, "contactsGroup");
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public Result save(@RequestBody String name){
        ContactsGroupEntity contactsGroupEntity = new ContactsGroupEntity();
        contactsGroupEntity.setName(name);
        contactsGroupEntity.setCreaterId(UserHolder.getUser().getId());

        contactsGroupService.save(contactsGroupEntity);

        return Result.success(null, "保存分组成功");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Result update(@RequestBody ContactsGroupEntity contactsGroup){
		contactsGroupService.updateById(contactsGroup);

        return Result.success(null, "");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public Result delete(@RequestBody Long[] ids){
		contactsGroupService.removeByIds(Arrays.asList(ids));

        return Result.success(null, "");
    }

    /**
     * 查询用户的分组
     * @param userId
     * @return
     */
    @GetMapping("/list/user/{userId}")
    public Result<List<ContactsGroupEntity>> getUserContactsGroup(@PathVariable Long userId){
        List<ContactsGroupEntity> contactsGroupEntityList = contactsGroupService.getUserContactsGroup(userId);

        return Result.success(contactsGroupEntityList, "获取用户联系人分组成功");
    }

}
